package com.cqjtu.eecs.utils.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cqjtu.eecs.entity.GraduateRequirementTemplate;
import com.cqjtu.eecs.entity.dto.GraduateRequirementTemplateDTO;
import com.cqjtu.eecs.service.IGraduateRequirementTemplateService;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class GraduateRequirementListener extends AnalysisEventListener<GraduateRequirementTemplateDTO> {
    private IGraduateRequirementTemplateService graduateRequirementTemplateService;

    private List<GraduateRequirementTemplate> list;

    // 版本编号
    private String trainingProjectVersionCode;

    // 专业代码
    private String trainingProjectMajorCode;

    public GraduateRequirementListener(String trainingProjectVersionCode,
                                String trainingProjectMajorCode ,
                                IGraduateRequirementTemplateService graduateRequirementTemplateService) {
        this.trainingProjectMajorCode = trainingProjectMajorCode;
        this.trainingProjectVersionCode = trainingProjectVersionCode;
        this.graduateRequirementTemplateService = graduateRequirementTemplateService;
        list = new ArrayList<>();
    }

    @Override
    public void invoke(GraduateRequirementTemplateDTO graduateRequirementTemplateDTO, AnalysisContext analysisContext) {
        if (graduateRequirementTemplateDTO.getGraduateRequirementCode() != null)
            list.add(new GraduateRequirementTemplate(
                    null,
                    graduateRequirementTemplateDTO.getGraduateRequirementCode(),
                    this.trainingProjectMajorCode,
                    this.trainingProjectVersionCode,
                    graduateRequirementTemplateDTO.getGraduateRequirementBrief(),
                    graduateRequirementTemplateDTO.getPreGraduateRequirementCode()
            ));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        graduateRequirementTemplateService.saveBatch(list);
        list.clear();
    }
}
